﻿using System;
using System.Reflection;

namespace SBPweb.Communication.Client.Library.Xmpp.Configuration
{

    partial class ConfigurationManager
    {

        #region Events

        public event EventHandler<AssemblyResolvingEventArgs> AssemblyResolvingFailed;

        #endregion

        #region Methods

        protected override void OnInitialize()
        {
            Assembly asm = Assembly.GetEntryAssembly();
            if(asm == null && AssemblyResolvingFailed != null)
            {
                AssemblyResolvingEventArgs area = new AssemblyResolvingEventArgs();
                AssemblyResolvingFailed(this, area);
                asm = area.Assembly;
            }
            ConfigurationSection cs = ConfigurationSection.OpenApplicationConfiguration(asm);

            messenger.Connections.ApplyConfiguration(cs.Connections);
            messenger.Contacts.ApplyConfiguration(cs.Contacts);
            messenger.Conversations.ApplyConfiguration(cs.Conversations);
            messenger.Profiles.ApplyConfiguration(cs.Profiles);
        }

        protected override void OnTerminate()
        {
        }

        #endregion

        #region Subclasses

        public class AssemblyResolvingEventArgs : EventArgs
        {

            public AssemblyResolvingEventArgs()
            {
            }

            public Assembly Assembly
            {
                get;
                set;
            }

        }

        #endregion

    }

}